package tau.dimes.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import tau.dimes.db.Database;
import tau.dimes.db.DatabaseException;
import tau.dimes.db.DatabaseFactory;

/**
 * Servlet implementation class DbTestServlet
 */
public class DbTestServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DbTestServlet()
	{
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException
	{
		PrintWriter writer = response.getWriter();
		Database db = DatabaseFactory.create("dimes1");
		try
		{
			ResultSet rs = db.executeQuery("SELECT * FROM DIMES.ipligence LIMIT 100");
			ResultSetMetaData md = rs.getMetaData();
			writer.print("<html><head></head><body><table border=1>");

			writer.write("<tr>");
			for (int i = 1; i <= md.getColumnCount(); i++)
				writer.write("<td>" + md.getColumnName(i) + "</td>");
			writer.write("</tr>");

			while (rs.next())
			{
				writer.write("<tr>");
				for (int i = 1; i <= md.getColumnCount(); i++)
					writer.write("<td>" + rs.getString(i) + "</td>");
				writer.write("</tr>");
			}

			writer.write("</table></body></html>");
			rs.close();
		}
		catch (DatabaseException e)
		{
			e.printStackTrace();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException
	{
	}
}
